package com.example.demo.controller;

import com.example.demo.bl.ApiService;
import com.example.demo.vo.InputVO;
import com.example.demo.vo.ResponseVO;
import com.example.demo.vo.SaveVO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

// @CrossOrigin(origins = {"http://localhost:8080", "http://xxxx:8080"})
@RestController
public class ApiController {

    @Autowired
    private ApiService apiService;

    @RequestMapping(path = "/api/analyze", method = RequestMethod.POST)
    public ResponseVO getParties(@RequestBody InputVO in) {
        Object data = apiService.analyseAndGetResult(in.getIn());
        if (data == null) return ResponseVO.buildFailed("分析发生错误");
        return ResponseVO.buildSuccess(data, "分析完成");
    }

    @RequestMapping(path = "/api/uploadjson",method = RequestMethod.POST)
    public ResponseVO save(@RequestBody SaveVO in){
        if (apiService.saveFile(in)) return ResponseVO.buildSuccess(in,"保存完成");
        return ResponseVO.buildFailed("保存失败");
    }
}
